package math.vectorSpace.R3.object;

import math.vectorSpace.R3.Point3D;
import math.vectorSpace.R3.Vector3D;

/**
 * The MovablePoint3D Class.
 *
 * Created: 7-Jan-2012
 * Last Modified: 7-Jan-2012
 * @author Shimu
 */
public class MovablePoint3D {

    /** The mass of this MovablePoint3D */
    private final double mass;

    /** The current location of this MovablePoint3D*/
    private Point3D position;

    /** The netForce on this MovablePoint3D at the current moment */
    private Vector3D netForce;

    /** The velocity of this MovablePoint3D at the current moment */
    private Vector3D velocity;

    public MovablePoint3D(Point3D startPoint, Vector3D initialVelocity,
            double mass) {
        this.mass = mass;

        this.position = startPoint;
        this.netForce = new Vector3D();
        this.velocity = initialVelocity;
    }

    public void applyForce(Vector3D force) {
        this.netForce = this.netForce.add(force);
    }

    public void update(double timeIncrement) {
        Vector3D acceleration = this.netForce.scale(1.0 / mass);
        // d = (vi)(t) + a(t^2)/2
        // d = (vi + vf)(t)/2
        Vector3D initialVelocity, finalVelocity, displacement;
        initialVelocity = this.velocity;
        finalVelocity = this.velocity.add(acceleration.scale(timeIncrement));
        displacement = (initialVelocity.add(finalVelocity)).scale(
                timeIncrement / 2.0);

        this.position = this.position.add(displacement);
        this.velocity = finalVelocity;
        this.netForce = new Vector3D();
    }

    public Point3D getPosition() {
        return this.position;
    }

    public Vector3D getVelocity() {
        return velocity;
    }
}
